package com.ruoyi.project.WebMoudle.ShopOffUser.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.project.WebMoudle.ImgFiles.domain.ImgFiles;
import com.ruoyi.project.WebMoudle.ImgFiles.service.IImgFilesService;
import com.ruoyi.project.WebMoudle.ShopSuser.domain.ShopSuser;
import com.ruoyi.project.WebMoudle.ShopSuser.service.IShopSuserService;
import com.ruoyi.project.WebMoudle.WxShop.controller.WxShopController;
import com.ruoyi.project.WebMoudle.WxShop.domain.WxShop;
import com.ruoyi.project.WebMoudle.WxShop.service.IWxShopService;
import com.ruoyi.project.WxMoudle.WebSocket.WebSocketServer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.ShopOffUser.domain.ShopOffUser;
import com.ruoyi.project.WebMoudle.ShopOffUser.service.IShopOffUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户注销商铺记录Controller
 * 
 * @author ruoyi
 * @date 2020-08-04
 */
@Controller
@RequestMapping("/ShopOffUser/ShopOffUser")
public class ShopOffUserController extends BaseController
{
    private String prefix = "ShopOffUser/ShopOffUser";

    @Autowired
    private IShopOffUserService shopOffUserService;
    @Autowired
    private IImgFilesService imgFilesService;
    @Autowired
    private IShopSuserService shopSuserService;
    @Autowired
    private WxShopController wxShopController;
    @Autowired
    private IWxShopService wxShopService;
    @Autowired
    private WebSocketServer webSocketServer;

    @RequiresPermissions("ShopOffUser:ShopOffUser:view")
    @GetMapping()
    public String ShopOffUser()
    {
        return prefix + "/ShopOffUser";
    }

    /**
     * 查询用户注销商铺记录列表
     */
    @RequiresPermissions("ShopOffUser:ShopOffUser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShopOffUser shopOffUser)
    {
        startPage();
        List<ShopOffUser> list = shopOffUserService.selectShopOffUserList(shopOffUser);
        return getDataTable(list);
    }

    /**
     * 导出用户注销商铺记录列表
     */
    @RequiresPermissions("ShopOffUser:ShopOffUser:export")
    @Log(title = "用户注销商铺记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShopOffUser shopOffUser)
    {
        List<ShopOffUser> list = shopOffUserService.selectShopOffUserList(shopOffUser);
        ExcelUtil<ShopOffUser> util = new ExcelUtil<ShopOffUser>(ShopOffUser.class);
        return util.exportExcel(list, "ShopOffUser");
    }

    /**
     * 新增用户注销商铺记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    @PostMapping("/queryOffShopCount")
    @ResponseBody
    public Object queryOffShopCount(){
       int i= shopOffUserService.queryOffShopCount();
       return i;
    }


    /**
     * 新增保存用户注销商铺记录
     */
    @RequiresPermissions("ShopOffUser:ShopOffUser:add")
    @Log(title = "用户注销商铺记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("manyFiles") MultipartFile[] manyFiles,ShopOffUser shopOffUser) throws IOException {
        if (manyFiles != null) {//有营业执照
            for (MultipartFile file : manyFiles) {
                String upload = FileUploadUtils.upload(file);
                ImgFiles img = new ImgFiles();
                img.setImgName(file.getOriginalFilename());
                img.setImgPath(upload);
                imgFilesService.insertImgFiles(img);
                if (StringUtils.isNotEmpty(shopOffUser.getFileIds())) {

                    shopOffUser.setFileIds(shopOffUser.getFileIds() + "," + img.getId());
                } else {
                    shopOffUser.setFileIds(img.getId() + "");
                }
            }
        }
        ShopSuser shopSuser = shopSuserService.selectOneShopId(getSysUser().getUserId());
        WxShop wxShop = wxShopService.selectWxShopById(shopSuser.getShopId());
        shopOffUser.setShopId(shopSuser.getShopId());
        shopOffUser.setCreateBy(getSysUser().getLoginName());
        shopOffUser.setShopCompany(wxShop.getShopCompany());
        int i = shopOffUserService.insertShopOffUser(shopOffUser);
        if (i>0){
            webSocketServer.sendAll("发送消息");
        }
        return toAjax(i);
    }

    /**
     * 查询是否存在申请中的记录
     * @return
     */
    @GetMapping("/queryOffShop")
    @ResponseBody
    public int queryOffShop()
    {
        ShopSuser shopSuser = shopSuserService.selectOneShopId(getSysUser().getUserId());
        ShopOffUser shopOffUser =new ShopOffUser();
        shopOffUser.setShopId(shopSuser.getShopId());
        shopOffUser.setStatus("0");
        List<ShopOffUser> list = shopOffUserService.selectShopOffUserList(shopOffUser);
        return list.size();
    }

    /**
     * 修改用户注销商铺记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ShopOffUser shopOffUser = shopOffUserService.selectShopOffUserById(id);
        mmap.put("shopOffUser", shopOffUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户注销商铺
     */
    @RequiresPermissions("ShopOffUser:ShopOffUser:edit")
    @Log(title = "用户注销商铺记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @Transactional
    public AjaxResult editSave(ShopOffUser shopOffUser) throws Exception {
        if (shopOffUser.getStatus().equals("4")){    //用户注销通过
            WxShop wxShop = wxShopService.selectWxShopById(shopOffUser.getShopId());
            shopOffUser.setShopCompany(wxShop.getShopCompany());
            webSocketServer.sendAll("发送消息");
            wxShopController.toOffShop(shopOffUser.getShopId(),"4");
        }
        shopOffUser.setHandBy(getLoginName());
        int i = shopOffUserService.updateShopOffUser(shopOffUser);
        if (i>0){
            webSocketServer.sendAll("发送消息");
        }
        return toAjax(i);
    }

    /**
     * 删除用户注销商铺记录
     */
    @RequiresPermissions("ShopOffUser:ShopOffUser:remove")
    @Log(title = "用户注销商铺记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shopOffUserService.deleteShopOffUserByIds(ids));
    }

}
